<!doctype html>
<html>
<head>
    <title>倔强的土豆</title>

    
        <meta charset="UTF-8">
<meta name="viewport", content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href='/public/css/screen.css'>
<link rel="stylesheet" href='/public/css/syntax.css'>
<link rel="stylesheet" href="/public/styles/default.css">
<script src="/public/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
    
</head>
<body>
    <div class="site">
        
            <div class="header">
            <div class="title">
    <a href="/">倔强的土豆</a>
    <a class="extra" href="/">home</a>
</div>
            </div>
        
        <!--
            <nav class="menu">
    <ul>
        <li>
            <a class="extra" href="/">分类</a></li>
        <li>
            <a class="extra" href="/">归档</a></li>
        <li>
            <a class="extra" href="/">标签</a></li>
    </ul>
</nav>
        -->
        
<div class="main">
    <p>javascript 字符串处理函数总结</p>
<p>&lt;!--more--&gt;
</p>
<h3 id="concat-">concat()</h3>
<p>组合两个或多个字符串，返回一个新的字符串。</p>
<pre><code class="lang-js">var a = &quot;hello&quot;;
var b = &quot;,world&quot;;
var c = a.concat(b);
alert(c);
//c = &quot;hello,world&quot;
</code></pre>
<h3 id="indexof-">indexOf()</h3>
<p>返回字符串中一个子串第一处出现的索引（从左到右搜索）。如果没有匹配项，返回 -1 。</p>
<pre><code class="lang-js">var index1 = a.indexOf(&quot;l&quot;);
// index1 = 2
var index2 = a.indexOf(&quot;l&quot;,3);
// index2 = 3
</code></pre>
<h3 id="charat-">charAt()</h3>
<p>返回指定位置的字符。</p>
<pre><code class="lang-js">var get_char = a.charAt(0);
//get_char = &quot;h&quot;
</code></pre>
<h3 id="lastindexof-">lastIndexOf()</h3>
<p>返回字符串中一个子串最后一处出现的索引（从右到左搜索），如果没有匹配项，返回 -1 。</p>
<pre><code class="lang-js">var index1 = lastIndexOf(&#39;l&#39;);
//index1 = 3
var index2 = lastIndexOf(&#39;l&#39;,2)
//index2 = 2
</code></pre>
<h3 id="match-">match()</h3>
<p>检查一个字符串匹配一个正则表达式内容，如果么有匹配返回 null。</p>
<pre><code class="lang-js">var re = new RegExp(/^/w+$/);
var is_alpha1 = a.match(re);
//is_alpha1 = &quot;hello&quot;
var is_alpha2 = b.match(re);
//is_alpha2 = null
</code></pre>
<h3 id="substring-">substring()</h3>
<p>返回字符串的一个子串，传入参数是起始位置和结束位置。</p>
<pre><code class="lang-js">var sub_string1 = a.substring(1);
//sub_string1 = &quot;ello&quot;
var sub_string2 = a.substring(1,4);
//sub_string2 = &quot;ell&quot;
</code></pre>
<h3 id="substr-">substr()</h3>
<p>返回字符串的一个子串，传入参数是起始位置和长度</p>
<pre><code class="lang-js">var sub_string1 = a.substr(1);
//sub_string1 = &quot;ello&quot;
var sub_string2 = a.substr(1,4);
//sub_string2 = &quot;ello&quot;
</code></pre>
<h3 id="replace-">replace()</h3>
<p>用来查找匹配一个正则表达式的字符串，然后使用新字符串代替匹配的字符串。</p>
<pre><code class="lang-js">var result1 = a.replace(re,&quot;Hello&quot;);
//result1 = &quot;Hello&quot;
var result2 = b.replace(re,&quot;Hello&quot;);
//result2 = &quot;,world&quot;
</code></pre>
<h3 id="search-">search()</h3>
<p>执行一个正则表达式匹配查找。如果查找成功，返回字符串中匹配的索引值。否则返回 -1 。</p>
<pre><code class="lang-js">var index1 = a.search(re);
//index1 = 0
var index2 = b.search(re);
//index2 = -1
</code></pre>
<h3 id="slice-">slice()</h3>
<p>提取字符串的一部分，并返回一个新字符串（与 substring 相同）。</p>
<pre><code class="lang-js">var sub_string1 = a.slice(1);
//sub_string1 = &quot;ello&quot;
var sub_string2 = a.slice(1,4);
//sub_string2 = &quot;ell&quot;
</code></pre>
<h3 id="split-">split()</h3>
<p>将字符串分离为数组</p>
<pre><code class="lang-js">var arr1 = a.split(&quot;&quot;);
//arr1 = [h,e,l,l,o]
</code></pre>
<h3 id="length-">length()</h3>
<p>返回字符串的长度，所谓字符串的长度是指其包含的字符的个数。</p>
<pre><code class="lang-js">var len = a.length();
//len = 5
</code></pre>
<h3 id="tolowercase">toLowerCase</h3>
<p>将整个字符串转成小写字母。</p>
<pre><code class="lang-js">var lower_string = a.toLowerCase();
//lower_string = &quot;hello&quot;
</code></pre>
<h3 id="touppercase">toUpperCase</h3>
<p>将整个字符串转成大写字母。</p>
<pre><code class="lang-js">var upper_string = a.toUpperCase();
//upper_string = &quot;HELLO&quot;
</code></pre>
<h3 id="-">去除空格</h3>
<pre><code class="lang-js">//去除左边的空格
String.prototype.LTrim = function()
{
    return this.replace(/(^/s*)/g, &quot;&quot;);
}

//去除右边的空格
String.prototype.Rtrim = function()
{
    return this.replace(/(/s*$)/g, &quot;&quot;);
}
</code></pre>
<p>&lt;!--
//去除右边的空格
===========================================
*/


/*
===========================================
//去除前后空格
===========================================
*/
String.prototype.Trim = function()
{
        return this.replace(/(^/s*)|(/s*$)/g, &quot;&quot;);
}

/*
===========================================
//得到左边的字符串
===========================================
*/
String.prototype.Left = function(len)
{

        if(isNaN(len)||len==null)
        {
                len = this.length;
        }
        else
        {
                if(parseInt(len)&lt;0||parseInt(len)&gt;this.length)
                {
                        len = this.length;
                }
        }

        return this.substr(0,len);
}


/*
===========================================
//得到右边的字符串
===========================================
*/
String.prototype.Right = function(len)
{

        if(isNaN(len)||len==null)
        {
                len = this.length;
        }
        else
        {
                if(parseInt(len)&lt;0||parseInt(len)&gt;this.length)
                {
                        len = this.length;
                }
        }

        return this.substring(this.length-len,this.length);
}


/*
===========================================
//得到中间的字符串,注意从0开始
===========================================
*/
String.prototype.Mid = function(start,len)
{
        return this.substr(start,len);
}


/*
===========================================
//在字符串里查找另一字符串:位置从0开始
===========================================
*/
String.prototype.InStr = function(str)
{

        if(str==null)
        {
                str = &quot;&quot;;
        }

        return this.indexOf(str);
}

/*
===========================================
//在字符串里反向查找另一字符串:位置0开始
===========================================
*/
String.prototype.InStrRev = function(str)
{

        if(str==null)
        {
                str = &quot;&quot;;
        }

        return this.lastIndexOf(str);
}

/*
===========================================
//计算字符串打印长度
===========================================
*/
String.prototype.LengthW = function()
{
        return this.replace(/[^/x00-/xff]/g,&quot;**&quot;).length;
}

/*
===========================================
//是否是正确的IP地址
===========================================
*/
String.prototype.isIP = function()
{

        var reSpaceCheck = /^(/d+)/.(/d+)/.(/d+)/.(/d+)$/;

        if (reSpaceCheck.test(this))
        {
                this.match(reSpaceCheck);
                if (RegExp.$1 &lt;= 255 &amp;&amp; RegExp.$1 &gt;= 0
                 &amp;&amp; RegExp.$2 &lt;= 255 &amp;&amp; RegExp.$2 &gt;= 0
                 &amp;&amp; RegExp.$3 &lt;= 255 &amp;&amp; RegExp.$3 &gt;= 0
                 &amp;&amp; RegExp.$4 &lt;= 255 &amp;&amp; RegExp.$4 &gt;= 0)
                {
                        return true;    
                }
                else
                {
                        return false;
                }
        }
        else
        {
                return false;
        }

}


/*
===========================================
//是否是正确的长日期
===========================================
*/
String.prototype.isLongDate = function()
{
        var r = this.replace(/(^/s*)|(/s*$)/g, &quot;&quot;).match(/^(/d{1,4})(-|//)(/d{1,2})/2(/d{1,2}) (/d{1,2}):(/d{1,2}):(/d{1,2})$/);
        if(r==null)
        {
                return false;
        }
        var d = new Date(r[1], r[3]-1,r[4],r[5],r[6],r[7]);
        return (d.getFullYear()==r[1]&amp;&amp;(d.getMonth()+1)==r[3]&amp;&amp;d.getDate()==r[4]&amp;&amp;d.getHours()==r[5]&amp;&amp;d.getMinutes()==r[6]&amp;&amp;d.getSeconds()==r[7]);

}

/*
===========================================
//是否是正确的短日期
===========================================
*/
String.prototype.isShortDate = function()
{
        var r = this.replace(/(^/s*)|(/s*$)/g, &quot;&quot;).match(/^(/d{1,4})(-|//)(/d{1,2})/2(/d{1,2})$/);
        if(r==null)
        {
                return false;
        }
        var d = new Date(r[1], r[3]-1, r[4]);
        return (d.getFullYear()==r[1]&amp;&amp;(d.getMonth()+1)==r[3]&amp;&amp;d.getDate()==r[4]);
}

/*
===========================================
//是否是正确的日期
===========================================
*/
String.prototype.isDate = function()
{
        return this.isLongDate()||this.isShortDate();
}

/*
===========================================
//是否是手机
===========================================
*/
String.prototype.isMobile = function()
{
        return /^0{0,1}13[0-9]{9}$/.test(this);
}

/*
===========================================
//是否是邮件
===========================================
*/
String.prototype.isEmail = function()
{
        return /^/w+((-/w+)|(/./w+))*/@[A-Za-z0-9]+((/.|-)[A-Za-z0-9]+)*/.[A-Za-z0-9]+$/.test(this);
}

/*
===========================================
//是否是邮编(中国)
===========================================
*/

String.prototype.isZipCode = function()
{
        return /^[//d]{6}$/.test(this);
}

/*
===========================================
//是否是有汉字
===========================================
*/
String.prototype.existChinese = function()
{
        //[/u4E00-/u9FA5]為漢字﹐[/uFE30-/uFFA0]為全角符號
        return /^[/x00-/xff]*$/.test(this);
}

/*
===========================================
//是否是合法的文件名/目录名
===========================================
*/
String.prototype.isFileName = function()
{
        return !/[/////*/?/|:&quot;&lt;&gt;]/g.test(this);
}

/*
===========================================
//是否是有效链接
===========================================
*/
String.prototype.isUrl = function()
{
        return /^http[s]?:////([/w-]+/.)+[/w-]+([/w-./?%&amp;=]*)?$/i.test(this);
}


/*
===========================================
//是否是有效的身份证(中国)
===========================================
*/
String.prototype.isIDCard = function()
{
        var iSum=0;
        var info=&quot;&quot;;
        var sId = this;

        var aCity={11:&quot;北京&quot;,12:&quot;天津&quot;,13:&quot;河北&quot;,14:&quot;山西&quot;,15:&quot;内蒙古&quot;,21:&quot;辽宁&quot;,22:&quot;吉林&quot;,23:&quot;黑龙江&quot;,31:&quot;上海&quot;,32:&quot;江苏&quot;,33:&quot;浙江&quot;,34:&quot;安徽&quot;,35:&quot;福建&quot;,36:&quot;江西&quot;,37:&quot;山东&quot;,41:&quot;河南&quot;,42:&quot;湖北&quot;,43:&quot;湖南&quot;,44:&quot;广东&quot;,45:&quot;广西&quot;,46:&quot;海南&quot;,50:&quot;重庆&quot;,51:&quot;四川&quot;,52:&quot;贵州&quot;,53:&quot;云南&quot;,54:&quot;西藏&quot;,61:&quot;陕西&quot;,62:&quot;甘肃&quot;,63:&quot;青海&quot;,64:&quot;宁夏&quot;,65:&quot;新疆&quot;,71:&quot;台湾&quot;,81:&quot;香港&quot;,82:&quot;澳门&quot;,91:&quot;国外&quot;};

        if(!/^/d{17}(/d|x)$/i.test(sId))
        {
                return false;
        }
        sId=sId.replace(/x$/i,&quot;a&quot;);
        //非法地区
        if(aCity[parseInt(sId.substr(0,2))]==null)
        {
                return false;
        }

        var sBirthday=sId.substr(6,4)+&quot;-&quot;+Number(sId.substr(10,2))+&quot;-&quot;+Number(sId.substr(12,2));

        var d=new Date(sBirthday.replace(/-/g,&quot;/&quot;))

        //非法生日
        if(sBirthday!=(d.getFullYear()+&quot;-&quot;+ (d.getMonth()+1) + &quot;-&quot; + d.getDate()))
        {
                return false;
        }
        for(var i = 17;i&gt;=0;i--)
        {
                iSum += (Math.pow(2,i) % 11) * parseInt(sId.charAt(17 - i),11);
        }

        if(iSum%11!=1)
        {
                return false;
        }
        return true;

}

/*
===========================================
//是否是有效的电话号码(中国)
===========================================
*/
String.prototype.isPhoneCall = function()
{
        return /(^[0-9]{3,4}/-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^/([0-9]{3,4}/)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/.test(this);
}


/*
===========================================
//是否是数字
===========================================
*/
String.prototype.isNumeric = function(flag)
{
        //验证是否是数字
        if(isNaN(this))
        {

                return false;
        }

        switch(flag)
        {

                case null:        //数字
                case &quot;&quot;:
                        return true;
                case &quot;+&quot;:        //正数
                        return                /(^/+?|^/d?)/d*/.?/d+$/.test(this);
                case &quot;-&quot;:        //负数
                        return                /^-/d*/.?/d+$/.test(this);
                case &quot;i&quot;:        //整数
                        return                /(^-?|^/+?|/d)/d+$/.test(this);
                case &quot;+i&quot;:        //正整数
                        return                /(^/d+$)|(^/+?/d+$)/.test(this);                       
                case &quot;-i&quot;:        //负整数
                        return                /^[-]/d+$/.test(this);
                case &quot;f&quot;:        //浮点数
                        return                /(^-?|^/+?|^/d?)/d*/./d+$/.test(this);
                case &quot;+f&quot;:        //正浮点数
                        return                /(^/+?|^/d?)/d*/./d+$/.test(this);                       
                case &quot;-f&quot;:        //负浮点数
                        return                /^[-]/d*/./d$/.test(this);               
                default:        //缺省
                        return true;                       
        }
}

/*
===========================================
//是否是颜色(#FFFFFF形式)
===========================================
*/
String.prototype.IsColor = function()
{
        var temp        = this;
        if (temp==&quot;&quot;) return true;
        if (temp.length!=7) return false;
        return (temp.search(//#[a-fA-F0-9]{6}/) != -1);
}

/*
===========================================
//转换成全角
===========================================
*/
String.prototype.toCase = function()
{
        var tmp = &quot;&quot;;
        for(var i=0;i&lt;this.length;i++)
        {
                if(this.charCodeAt(i)&gt;0&amp;&amp;this.charCodeAt(i)&lt;255)
                {
                        tmp += String.fromCharCode(this.charCodeAt(i)+65248);
                }
                else
                {
                        tmp += String.fromCharCode(this.charCodeAt(i));
                }
        }
        return tmp
}

/*
===========================================
//对字符串进行Html编码
===========================================
*/
String.prototype.toHtmlEncode = function()
{
        var str = this;

        str=str.replace(/&amp;/g,&quot;&amp;&quot;);
        str=str.replace(/&lt;/g,&quot;&lt;&quot;);
        str=str.replace(/&gt;/g,&quot;&gt;&quot;);
        str=str.replace(//&#39;/g,&quot;&apos;&quot;);
        str=str.replace(//&quot;/g,&quot;&quot;&quot;);
        str=str.replace(//n/g,&quot;&lt;br&gt;&quot;);
        str=str.replace(// /g,&quot;&nbsp;&quot;);
        str=str.replace(//t/g,&quot;&nbsp;&nbsp;&nbsp;&nbsp;&quot;);

        return str;
}

/*
===========================================
//转换成日期
===========================================
*/
String.prototype.toDate = function()
{
        try
        {
                return new Date(this.replace(/-/g, &quot;//&quot;));
        }
        catch(e)
        {
                return null;
        }
}
--&gt;
</p>
<h3 id="-">参考链接</h3>
<p><a href="http://blog.csdn.net/cyai/article/details/4213956">人生若可如罗盘，就不需迷茫</a></p>

</div>

        
            <div class="footer">
    <div class="contact">
        <p>
            <!--jjtudou-->
            <!--<br /> -->
            <!--<a href="https://codestarter.org/">Codestarter</a>,
            <a href="https://github.com/">GitHub</a>-->
            QQ:438678287 [SHINE TU] 
        </p>
    </div>
    <!--
    <div class="contact">
        <p>
            <a href="http://github.com/mojombo/">github.com/mojombo</a><br />
            <a href="http://twitter.com/mojombo/">twitter.com/mojombo</a><br />
        </p>
    </div>-->
    <!--<div class="rss">
        <a href="http://feeds.feedburner.com/tom-preston-werner">
            <img src="/images/rss.png" alt="Subscribe to RSS Feed" />
        </a>
    </div>-->
</div>
        
    </div>
</body>
</html>